package jeecg.business.service.impl.chat;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import jeecg.business.entity.chat.ChatInfoEntity;
import jeecg.business.service.chat.ChatInfoServiceI;

import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.common.service.impl.CommonServiceImpl;
import org.jeecgframework.core.util.StringUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("chatInfoService")
@Transactional
public class ChatInfoServiceImpl extends CommonServiceImpl implements ChatInfoServiceI {
	public void save(List<ChatInfoEntity> infolist){
		Connection conn = null;
		PreparedStatement ps = null;
		PreparedStatement dps = null;
		
		/**
		 * 过滤
		 */
		if(infolist != null){
			for(int i=0;i<infolist.size();i++){
				if("10000".equals(infolist.get(i).getQqnum()))		//去掉10000号
					infolist.remove(i--);
			}
				
		}
		System.out.println(infolist.size());
		String dsql = " truncate table QQ_CHAT_INFO ";
		try{
			conn = getJdbcTemplate().getDataSource().getConnection();
			conn.setAutoCommit(false);
			dps = conn.prepareStatement(dsql);
			String sql = " insert into QQ_CHAT_INFO values (? , ? , ? , ? , ? , ?) ";
			ps = conn.prepareStatement(sql);
			dps.execute();
			List<String> errorList = new ArrayList<String>();
			if(infolist != null){
				int row = 0;
				int errornum = 0;
				for(ChatInfoEntity c : infolist){
					row ++ ;
					try{	//单次错误直接掉过
						ps.setObject(1, UUID.randomUUID().toString());
						ps.setObject(2, c.getQqnum());
						ps.setObject(3, c.getQqGroupName());
						ps.setObject(4, c.getNickname().replace("?", ""));
						ps.setObject(5, c.getTime());
						ps.setObject(6, c.getInfo().trim().replace("?", ""));
						ps.execute();
					}catch(Exception e){
//						e.printStackTrace();
						errornum ++;
//						System.out.println(errornum);
						errorList.add(c.getInfo());
					}
					if(row%10000==0)
						System.out.println(row);
				}	
				System.out.println(errornum);
			}
			conn.commit();
			
//			for(String errmsg : errorList)
//				System.out.println(errmsg);
		}catch(Exception e){
			e.printStackTrace();
			try {
				conn.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		}finally{
			try{
				if(ps != null)
					ps.close();
				if(conn != null)
					conn.close();
			}catch(Exception e){
				e.printStackTrace();
			}
		}
	}
	
	public boolean has(ChatInfoEntity info){
		String sql = " select count(1)  from QQ_CHAT_INFO where qqnum = ? and time = ? ";
		return getJdbcTemplate().queryForInt(sql, info.getQqnum() , info.getTime())>=1;
	}
	
	public DataGrid list(ChatInfoEntity chat , String starttime , String endtime , int pageNum){
		String hql = " select i from ChatInfoEntity i  where 1=1 ";
		
		String csql = " select count(1) from QQ_CHAT_INFO i where 1=1 " ;
		
		if(chat != null){
			if(StringUtil.isNotEmpty(chat.getQqnum())){
				hql += " and i.qqnum like '%"+chat.getQqnum()+"%' ";
				csql += " and i.qqnum like '%"+chat.getQqnum()+"%' ";
			}
			
			if(StringUtil.isNotEmpty(chat.getNickname())){
				hql += " and i.nickname like '%"+chat.getNickname()+"%' ";
				csql += " and i.nickname like '%"+chat.getNickname()+"%' ";
			}
			
			if(StringUtil.isNotEmpty(chat.getQqGroupName())){
				hql += " and i.qqGroupName like '%"+chat.getQqGroupName()+"%' ";
				csql += " and i.qqGroupName like '%"+chat.getQqGroupName()+"%' ";
			}
			
			if(StringUtil.isNotEmpty(chat.getInfo())){
				hql += " and i.info like '%"+chat.getInfo()+"%' ";
				csql += " and i.info like '%"+chat.getInfo()+"%' ";
			}
			
			if(StringUtil.isNotEmpty(starttime)){
				hql += " and i.time >= '"+starttime+"' ";
				csql += " and i.time >= '"+starttime+"' ";
			}
			
			if(StringUtil.isNotEmpty(endtime)){
				hql += " and i.time <= '"+endtime+"' ";
				csql += " and i.time <= '"+endtime+"' ";
			}
		}
		
		hql += " order by i.time desc ";
		Integer count = getJdbcTemplate().queryForInt(csql);
		DataGrid data = new DataGrid();
		data.setTotal(count);
		data.setPage(pageNum);
		data.setRows(50);
		data.setReaults(findByQueryString(hql, (pageNum-1)*50, 50));
		return data;
	}
	
	public List<ChatInfoEntity> list(String qqGroupName , String start , String end){
		String hql = " from ChatInfoEntity where qqgroupname = '"+qqGroupName+"' and time >= '"+start+"' and time <= '"+end+"' order by time desc ";
		return findByQueryString(hql);
	}
}